<?php
/**
 * Created by PhpStorm.
 * User: zhongbaitong
 * Date: 2020/5/20
 * Time: 20:11
 */

namespace app\controller;

use sys\Controller;

class Base extends Controller
{

    public $user = null;

    public function __construct()
    {
        $this->user = $this->getUserInfo();
    }

    /**
     * 获取当前登录用户信息
     * @return mixed|null
     */
    public function getUserInfo()
    {
        $user_info = $_COOKIE["user_info"] ?? null;
        if ($user_info) {
            $user_info = unserialize($user_info);
        }
        return $user_info;
    }

    /**
     * 校验是否登录
     */
    public function isLogin()
    {
        if (isset($_SESSION['isLogin']) && $_SESSION['isLogin'] && !!$this->user) {
            return true;
        }
        return false;
    }

    /**
     * 简单的输入过滤及验证
     * @param array|string $must 参数必须且不许为""、null
     * @return string
     */
    public function getInput($must = [])
    {
        if ($input = $_REQUEST) {
            foreach ($input as $k => $v) {
                // addslashes加反斜线，配置项 magic_quotes_gpc=On 时会自动加上
                // htmlspecialchars转义 html 标签，对比 htmlentities
                is_string($v) && $input[$k] = htmlspecialchars(addslashes(trim($v)));
            }
        }

        if ($must) {
            if (is_string($must)) {
                $must = explode(',', $must);
            }
            if (is_array($must)) {
                foreach ($must as $k => $v) {
                    if (!isset($input[$v]) || null === $input[$v] || "" === $input[$v]) {
                        echo json_encode([
                            "code" => 0,
                            "msg" => "lack of params: [" . $v . "]"
                        ], JSON_UNESCAPED_UNICODE);
                        die;
                    }
                }
            }
        }
        return $input;
    }

    /**
     * API::统一的返回结果
     * @param array $params
     * @return false|string
     */
    public function apiReturn($params = [])
    {
        if (is_array($params) && isset($params['code'], $params['msg'])) {
            $result = $params;
        } else if ($params !== '') {
            $result = ['code' => 1, 'msg' => '操作成功!', 'data' => $params];
        } else {
            $result = ['code' => 0, 'msg' => '操作失败!'];
        }
        return json($result);
    }

    /**
     * API::成功时返回
     * @param string $msg
     * @param array $data
     * @param int $code
     * @return false|string
     */
    public function apiSuccess($msg = '操作成功!', $data = [], $code = 1)
    {
        $result = compact('code', 'msg');
        $data && $result['data'] = $data;

        return $this->apiReturn($result);
    }

    /**
     * API::失败时返回
     * @param string $msg
     * @param array $data
     * @param int $code
     * @return false|string
     */
    public function apiError($msg = "操作失败！", $data = [], $code = 0)
    {
        $result = compact('code', 'msg');
        $data && $result['data'] = $data;

        return $this->apiReturn($result);
    }

}